package n001;

import java.util.Arrays;

/**
 * <a href="https://leetcode.cn/problems/corporate-flight-bookings/">1109. 航班预订统计</a>
 */
public class _1109_FLightReserve {

    public static void main(String[] args) {
        _1109_FLightReserve fLightReserve = new _1109_FLightReserve();
        int[][] bookings = {
                {1,2,10},{2,3,20},{2,5,25}
        };
        System.out.println(Arrays.toString(fLightReserve.corpFlightBookings(bookings, 5)));
    }

    public int[] corpFlightBookings(int[][] bookings, int n) {
        // 差分数组
        int[] statistical = new int[n + 1];
        for (int[] booking : bookings) {
            int start = booking[0] - 1;
            int end = booking[1];
            int count = booking[2];
            statistical[start] += count;    //
            statistical[end] -= count;
        }
        int[] prefixSum = new int[n];
        prefixSum[0] = statistical[0];
        for (int i = 1; i < n; i++) {
            prefixSum[i] = statistical[i] + prefixSum[i - 1];
        }
        return prefixSum;
    }

}
